package com.whq.thrift.api.web.admin.service.admin.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whq.thrift.api.common.spring.datasource.DataSourceContextHolder;
import com.whq.thrift.api.web.admin.db.admin.PermissionUri;
import com.whq.thrift.api.web.admin.db.admin.UserInfo;
import com.whq.thrift.api.web.admin.db.admin.UserInfoMapper;
import com.whq.thrift.api.web.admin.db.admin.UserMermission;
import com.whq.thrift.api.web.admin.service.admin.UserInfoService;

@Service
public class UserInfoServiceImpl implements UserInfoService {
	@Autowired
	private UserInfoMapper userInfoMapper;

	public void addRecord(UserInfo record) {
		DataSourceContextHolder.set("dataSourceAdmin");
		if (record == null) {
			return;
		}
		Date now = new Date();
		record.setCreateTime(now);
		record.setUpdateTime(now);

		this.userInfoMapper.addRecord(record);
	}

	public List<UserInfo> findRecordsByMap(Map<String, Object> whereParams, int beginIndex, int maxCount) {
		DataSourceContextHolder.set("dataSourceAdmin");
		if (whereParams == null) {
			whereParams = new HashMap<String, Object>(0);
		}

		whereParams.put("beginIndex", beginIndex);
		whereParams.put("maxCount", maxCount);

		return this.userInfoMapper.getSelectRecrodByMap(whereParams);
	}

	public void updateRecord(UserInfo record) {
		DataSourceContextHolder.set("dataSourceAdmin");
		if (record == null) {
			return;
		}

		Date now = new Date();
		record.setUpdateTime(now);
		this.userInfoMapper.updRecord(record);
	}

	public void deleteRecord(String id) {
		DataSourceContextHolder.set("dataSourceAdmin");
		this.userInfoMapper.delRecord(id);
	}

	public int findRecordCount(Map<String, Object> whereParams) {
		DataSourceContextHolder.set("dataSourceAdmin");
		return this.userInfoMapper.findRecordsCount(whereParams);
	}

	public UserInfo findRecordById(String id) {
		DataSourceContextHolder.set("dataSourceAdmin");
		return this.userInfoMapper.getSelectRecrodByKey(id);
	}

	public List<PermissionUri> findPermissionByMap() {
		DataSourceContextHolder.set("dataSourceAdmin");
		return this.userInfoMapper.findPermissionUriByMap();
	}

	public List<UserMermission> findUserMermissionByEmail(String email) {
		DataSourceContextHolder.set("dataSourceAdmin");
		
		return this.userInfoMapper.findUserMermissionByEmail(email);
	}

	public void delUserPermission(String email){
		DataSourceContextHolder.set("dataSourceAdmin");
		this.userInfoMapper.delUserPermission(email);
	}
	
	public void addUserMermission(UserMermission record) {
		DataSourceContextHolder.set("dataSourceAdmin");
		if(record==null){
			return;
		}
		
		this.userInfoMapper.addUserPermission(record);
		
	}

}